package com.qiaoyanqing.ximalaya.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.qiaoyanqing.ximalaya.downloadmodule.download_fragment.DownLoadFragment;
import com.qiaoyanqing.ximalaya.palymodule.play_fragment.CenterButtonFragment;
import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_fragment.DiscoverFragment;

import com.qiaoyanqing.ximalaya.minemodule.mine_fragment.MineFragment;
import com.qiaoyanqing.ximalaya.subscribemodule.subscribe_fragment.SubscribeFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    //发现页面
    private DiscoverFragment mDiscoverFragment;
    //我的页面
    private MineFragment mMineFragment;
    //下载页面
    private DownLoadFragment mDownloadFragment;
    //订阅页面
    private SubscribeFragment mSubscribeFragment;
    //中间的，凸起来的页面,
    private CenterButtonFragment mCenterButtonFragment;

    private FragmentManager mManager;
    //记录当前显示的Fragment
    private Fragment mCurrentFragment;

    private RadioGroup mGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mGroup = (RadioGroup) findViewById(R.id.main_group);

        mDiscoverFragment = new DiscoverFragment();
        mManager = getSupportFragmentManager();
        mManager.beginTransaction()
                .add(R.id.main_fragment, mDiscoverFragment, "DiscoverFragment")
                .commit();
        mCurrentFragment = mDiscoverFragment;

        //设置监听，点击每一个button显示不同的Fragment
        mGroup.setOnCheckedChangeListener(this);

    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        mManager = getSupportFragmentManager();
        switch (i) {
            case R.id.main_btn_dicover:
                mManager.beginTransaction()
                        .hide(mCurrentFragment)
                        .show(mDiscoverFragment)
                        .commit();
                mCurrentFragment = mDiscoverFragment;
                break;
            case R.id.main_btn_dingyue:
                if (mSubscribeFragment == null) {
                    mSubscribeFragment = new SubscribeFragment();
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment, mSubscribeFragment, "mSubscribeFragment")
                            .commit();
                } else {
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mSubscribeFragment)
                            .commit();
                }
                mCurrentFragment = mSubscribeFragment;

                break;
            case R.id.main_btn_center:
                if (mCenterButtonFragment == null) {
                    mCenterButtonFragment = new CenterButtonFragment();
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment, mCenterButtonFragment, "mCenterButtonFragment")
                            .commit();
                } else {
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mCenterButtonFragment)
                            .commit();
                }
                mCurrentFragment = mCenterButtonFragment;
                break;
            case R.id.main_btn_download:

                if (mDownloadFragment == null) {
                    mDownloadFragment = new DownLoadFragment();
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment, mDownloadFragment, "mDownloadFragment")
                            .commit();
                } else {
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mDownloadFragment)
                            .commit();
                }
                mCurrentFragment = mDownloadFragment;

                break;
            case R.id.main_btn_mine:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .add(R.id.main_fragment, mMineFragment, "mMineFragment")
                            .commit();
                } else {
                    mManager.beginTransaction()
                            .hide(mCurrentFragment)
                            .show(mMineFragment)
                            .commit();
                }
                mCurrentFragment = mMineFragment;
                break;
        }
    }

}
